package udp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.net.InetSocketAddress;
import java.util.List;

public class ServerEventEncoder extends MessageToMessageEncoder<String> {

    private final InetSocketAddress remoteAddress;


    public ServerEventEncoder(InetSocketAddress remoteAddress) {
        this.remoteAddress = remoteAddress;
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, String s, List<Object> out) throws Exception {

        byte[] reqMsgByte = s.getBytes("UTF-8");

        ByteBuf reqByteBuf = channelHandlerContext.alloc().buffer(s.length());

        reqByteBuf.writeBytes(reqMsgByte);

            out.add(new DatagramPacket(reqByteBuf,remoteAddress));
    }
}
